package com.ruoyi.ucar.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.ucar.domain.TRechargeRecord;
import com.ruoyi.ucar.domain.po.RechargeRecordDetailQueryPo;
import com.ruoyi.ucar.domain.po.RechargeRecordListQueryPo;
import com.ruoyi.ucar.moreq.RechargeRecordDetailQueryMoReq;
import com.ruoyi.ucar.moreq.RechargeRecordListQueryMoReq;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * 充值记录Mapper接口
 * 
 * @author sungangbin
 * @date 2025-05-13
 */
@Mapper
public interface TRechargeRecordMapper extends BaseMapper<TRechargeRecord>
{
    /**
     * 查询充值记录
     * 
     * @param id 充值记录主键
     * @return 充值记录
     */
    public TRechargeRecord selectTRechargeRecordById(Long id);

    /**
     * 查询充值记录列表
     * 
     * @param tRechargeRecord 充值记录
     * @return 充值记录集合
     */
    public List<TRechargeRecord> selectTRechargeRecordList(TRechargeRecord tRechargeRecord);

    /**
     * 新增充值记录
     * 
     * @param tRechargeRecord 充值记录
     * @return 结果
     */
    public int insertTRechargeRecord(TRechargeRecord tRechargeRecord);

    /**
     * 修改充值记录
     * 
     * @param tRechargeRecord 充值记录
     * @return 结果
     */
    public int updateTRechargeRecord(TRechargeRecord tRechargeRecord);

    /**
     * 删除充值记录
     * 
     * @param id 充值记录主键
     * @return 结果
     */
    public int deleteTRechargeRecordById(Long id);

    /**
     * 批量删除充值记录
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteTRechargeRecordByIds(Long[] ids);


    @Select("<script>" +
             "   SELECT  " +
            "    r.id as id,  " +
            "    r.recharge_record_uuid as rechargeRecordUuid,  " +
            "    r.recharge_type as rechargeType,  " +
            "    r.recharge_money as rechargeMoney,  " +
            "    r.give_money as giveMoney,  " +
            "    r.recharge_status as rechargeStatus,  " +
            "    r.recharge_datetime as rechargeDatetime,  " +
            "    c.customer_name as customerName,  " +
            "    c.customer_uuid as customerUuid,  " +
            "    c.customer_phone as customerPhone  " +
            "    FROM  " +
            "  t_recharge_record r  " +
            "  LEFT JOIN t_customer c ON r.customer_uuid = c.customer_uuid"+
            "<where>" +
            "   <if test='queryParams.deleteFlag != null'>" +
            "       AND (r.delete_flag = #{queryParams.deleteFlag} " +
            "       AND c.delete_flag = #{queryParams.deleteFlag}) " +
            "   </if>" +
            "   <if test='queryParams.customerName != null and queryParams.customerName != \"\"'> AND c.customer_name LIKE CONCAT('%',#{queryParams.customerName},'%') </if>" +
            "   <if test='queryParams.customerPhone != null'> AND c.customer_phone = #{queryParams.customerPhone} </if>" +
            "   <if test='queryParams.rechargeStatus != null'> AND r.recharge_status = #{queryParams.rechargeStatus} </if>" +
            "   <if test='queryParams.rechargeType != null'> AND r.recharge_type = #{queryParams.rechargeType} </if>" +
            "   <if test='queryParams.rechargeStartDate != null and queryParams.rechargeEndDate != null'> AND r.recharge_datetime &gt;= #{queryParams.rechargeStartDate} AND r.recharge_datetime &lt;= #{queryParams.rechargeEndDate} </if>" +
            "</where>" +
            " order by r.id desc "+
            "</script>")
    public IPage<RechargeRecordListQueryPo> rechargeRecordListQueryPage(IPage<RechargeRecordListQueryPo> page, @Param("queryParams")RechargeRecordListQueryMoReq rechargeRecordListQueryMoReq);

    @Select("<script>" +
            "   SELECT  " +
            "    r.id as id,  " +
            "    r.recharge_record_uuid as rechargeRecordUuid,  " +
            "    r.recharge_type as rechargeType,  " +
            "    r.recharge_money as rechargeMoney,  " +
            "    r.give_money as giveMoney,  " +
            "    r.recharge_status as rechargeStatus,  " +
            "    r.recharge_datetime as rechargeDatetime,  " +
            "    c.customer_name as customerName,  " +
            "    c.customer_uuid as customerUuid,  " +
            "    c.customer_phone as customerPhone  " +
            "    FROM  " +
            "  t_recharge_record r  " +
            "  LEFT JOIN t_customer c ON r.customer_uuid = c.customer_uuid"+
            "<where>" +
            "   <if test='queryParams.deleteFlag != null'>" +
            "       AND (r.delete_flag = #{queryParams.deleteFlag} " +
            "       AND c.delete_flag = #{queryParams.deleteFlag}) " +
            "   </if>" +
            "   <if test='queryParams.id != null'> AND r.id = #{queryParams.id} </if>" +
            "   <if test='queryParams.rechargeRecordUuid != null and queryParams.rechargeRecordUuid != \"\"'> AND r.recharge_record_uuid = #{queryParams.rechargeRecordUuid} </if>" +
            "</where>" +
            "</script>")
    public List<RechargeRecordDetailQueryPo> rechargeRecordDetailQuery(@Param("queryParams") RechargeRecordDetailQueryMoReq rechargeRecordDetailQueryMoReq);


    public Integer judgeCustomerRechargeRecord(HashMap<String,Object> map );

    public BigDecimal queryFirstRechargeRecord(HashMap<String,Object> map );
}
